package com.ysd.lis.service.iqc.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.iqc.*;
import com.ysd.lis.mapper.iqc.LimsIQCResultsMapper;
import com.ysd.lis.request.CommonPageDto;
import com.ysd.lis.service.iqc.LimsIQCResultsService;
import com.ysd.lis.service.iqc.QcMaterialItemResultGdbService;
import com.ysd.util.ToolsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * cold_chain.t_user 服务实现类
 * </p>
 *
 * @author peng
 * @since 2024-01-08
 */
@Service
@Slf4j
public class LimsIQCResultsServiceImpl extends ServiceImpl<LimsIQCResultsMapper, LimsIQCResults> implements LimsIQCResultsService {


    @Autowired
    LimsIQCResultsMapper limsIQCResultsMapper;
    @Autowired
    QcMaterialItemResultGdbService qcMaterialItemResultGdbService;

    @Override
    public Result syncQcMaterialItemResultGdb(CommonPageDto pageParam) {
        List<LimsIQCResults> gdbLisMaterailItemResultList = this.getGdbLisMaterailItemResultList();
//        for(LimsIQCResults iqcResults: gdbLisMaterailItemResultList){
//            QcMaterialItemResultGdb byId = qcMaterialItemResultGdbService.getById(iqcResults.getId());
//            if(ToolsUtils.isEmpty(byId)){
//                QcMaterialItemResultGdb gdb = new QcMaterialItemResultGdb();
//                BeanUtils.copyProperties(iqcResults,gdb);
//                qcMaterialItemResultGdbService.save(gdb);
//            }
//        }
        List<QcMaterialItemResultGdb> list = qcMaterialItemResultGdbService.list();
        List<QcMaterialItemResultGdb> saveList = new ArrayList<>();
        for(LimsIQCResults iqcResults: gdbLisMaterailItemResultList){
            long count = list.stream().filter(a -> a.getId().equals(iqcResults.getId())).count();
            if(count==0){
                QcMaterialItemResultGdb gdb = new QcMaterialItemResultGdb();
                BeanUtils.copyProperties(iqcResults,gdb);
                gdb.setIsSync(false);
                saveList.add(gdb);
            }
        }
        if(ToolsUtils.isNotEmpty(saveList)){
            qcMaterialItemResultGdbService.saveBatch(saveList);
        }
        return null;
    }



    private List<LimsIQCResults> getGdbLisMaterailItemResultList(){
        MPJLambdaWrapper<LimsIQCResults> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(LimsIQCResults.class)
                .selectAs("ins", LimsInss::getCode,LimsIQCResults::getInstrCode)
                .selectAs("le", LimsIQCMedicineDecisionLevels::getCode,LimsIQCResults::getHorizontal)
                .selectAs("ans", LimsAnss::getCode,LimsIQCResults::getItemNo)
                .leftJoin(LimsInss.class,"ins",p->p.eq(LimsInss::getId,LimsIQCResults::getInsId))
                .leftJoin(LimsAnss.class,"ans",p->p.eq(LimsAnss::getId,LimsIQCResults::getAnsId))
                .leftJoin(LimsIQCMedicineDecisionLevels.class,"le",p->p.eq(LimsIQCMedicineDecisionLevels::getId,LimsIQCResults::getIQCMedicineDecisionLevelId))
                .leftJoin(LimsIQCMaterials.class,"qm",p->p.eq(LimsIQCMaterials::getId,LimsIQCResults::getIQCMaterialId))
                .leftJoin(LimsIQCMaterialBatches.class,"mb",p->p.eq(LimsIQCMaterialBatches::getId,LimsIQCResults::getIQCMaterialBatchId))
                .leftJoin(LimsIQCMaterialInsAnss.class,"ma",p->p.eq(LimsIQCMaterialInsAnss::getIQCMaterialId,LimsIQCResults::getIQCMaterialId).eq(LimsIQCMaterialInsAnss::getInsAnsId,LimsIQCResults::getInsAnsId))
                .eq(LimsIQCResults::getIsDeleted,0)
                .eq(LimsIQCMaterials::getIsDeleted,0)
                .eq(LimsIQCMaterialBatches::getIsDeleted,0)
                .eq(LimsIQCMaterialInsAnss::getIsDeleted,0);

        List<LimsIQCResults> limsIQCResults = limsIQCResultsMapper.selectList(queryWrapper);
        return limsIQCResults;
    }


}
